// pages/prescription_flow//order_msg/order_msg.js
import $service from '../../../service.js';
var app = getApp();
var order_id;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    hisType: $service.isHis,
    allList:[],
    userList:"",
    orderInfo: {},
    flag: 0,
    Dflag:0,
    flish:0,
    isExpress:true,  
    playFlag:false,
    prescriptionDetail: {},
    prescriptionList: [],
    deliveryInfo: {
      status: '',
      time: ""
    },
    hisTabList: [{
      name: '全部',
      index: ''
    }, {
      name: '待付款',
      index: 0
    }, {
      name: '已支付',
      index: 1
    },  {
      name: '待自提',
      index: 2
    }, {
      name: '已完成',
      index: 3
    }, {
      name: '已取消',
      index: -1
    },],
    tabList: [{
      name: '待付款',
      index: 10
    },{
      name: '待发货',
      index: 20
    },{
      name: '待自提',
      index: 35
    },{
      name: '待收货',
      index: 30
    },{
      name: '已完成',
      index: 40
    },{
      name: '已取消',
      index: 0
    }]
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    order_id = e.prescription_id;
    if (this.data.hisType) {
      this.data.type = e.type
    }
    this.setData({
      userList: JSON.parse($service.getStore('userData')),
      type: this.data.type
    })
  },
  onShow: function () {
    if (this.data.type == 1) {
      this.getPrescriptionInfoById()
    } else {
      this.orderDetail()
      this.queryDeliveryInfo()
    }
  },
   //查看地图
   viewMap(e){
     let lat = Number(this.data.orderInfo.loc_x);
     let lng = Number(this.data.orderInfo.loc_y);
      wx.openLocation({
        latitude:lat,
        longitude:lng,
        scale: 18,
        name:e.target.dataset.text,
        address:this.data.orderInfo.group_address || ""
      })
   },
  // 获取评价
  getCom: function (order_id) {
    var jsonData = {
      "actName": "GetComment",
      "order_id": order_id,
    }
    var jsonAjax = {
      "url": "/nethospnew/service/nethosp_action.ashx",
      "jsonData": jsonData,
    }
    $service.getAjaxData(jsonAjax, this.getcomOK)
  },
  getcomOK: function (jsonBack) {
    var that = this
    // if (jsonBack.list[0].create_time!=''){
    if (jsonBack.code != '0'){
      that.setData({
        flag: jsonBack.list[0].store_score,
        Dflag: jsonBack.list[0].log_score,
        comstate:0,
        flish: 1
      })
    }else{
      that.setData({
        comstate:1,
      })
    }
  },
  //提交评价
  postCom: function (){
    var jsonData = {
      "actName": "DoComment",
      "order_id": order_id,
      "store_score": this.data.flag,
      "log_score": this.data.Dflag,
    }
    var jsonAjax = {
      "url": "/nethospnew/service/nethosp_action.ashx",
      "jsonData": jsonData,
    }
    $service.getAjaxData(jsonAjax, this.postComOK)
  },
  postComOK: function (jsonBack) {
    var that = this
    if (jsonBack.code== 1) {
      // this.onload()
      that.getCom(order_id);
    }
  },

  // 获取订单信息 处方详情
  orderDetail() {
    app.http.orderDetail({order_num: order_id}).then(res => {
      this.setData({
        orderInfo: res.data
      })
      return app.http.getPrescriptioninfo({prescriptionNo: res.data.rp_id}) // 获取处方详情
    }).then(res => {
      this.setData({
        prescriptionDetail: res.data,
        prescriptionList: res.data.prescriptionType === '1' ? res.data.chineseMedicineResqList : res.data.westernMedicineResqList 
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },

  // 确认收货
  confirm_indent() {
    let params = {
      order_num: order_id,
      type: 2
    }
    app.http.saveOrder(params).then(res => {
      this.orderDetail()
      $service.showToast('收货成功')
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },

  // 继续支付
  wxDoPay() {
    wx.login({
      success: (res) => {
        let params = {
          "code": res.code,
          "money": Number(this.data.orderInfo.indent_total),
          "orderSn": order_id
        }
        app.http.initiatePayment(params).then(res => {
          let {timeStamp, nonceStr, signType, paySign, packageWx} = res.data;
              wx.requestPayment({
                timeStamp,
                nonceStr,
                package: packageWx,
                signType,
                paySign,
                success: (res) => {
                  this.orderDetail()
                  $service.showToast('支付成功')
                },
              })
        }).catch(err => {
          $service.showToast(err.msg)
        })
      }
    })
  },
  // 退款
  Cancel_indent() {
    wx.showModal({
      content: "请联系客服，客服热线：0668-2900020；待和客服确认后，退款会在一到两个工作日后按原路退回微信账号，请留意微信通知提醒！",
      showCancel: false,
      confirmText: '知道了',
      success (res) {
        if (res.confirm) {
          //不操作
        }
      }
    })
  },
  // 改变星星颜色
  changeColor1: function () {
    var that = this;
    that.setData({
      flag: 1
    });
  },
  changeColor2: function () {
    var that = this;
    that.setData({
      flag: 2
    });
  },
  changeColor3: function () {
    var that = this;
    that.setData({
      flag: 3
    });
  },
  changeColor4: function () {
    var that = this;
    that.setData({
      flag: 4
    });
  },
  changeColor5: function () {
    var that = this;
    that.setData({
      flag: 5
    });
  },
  // 改变星星颜色2
  DchangeColor1: function () {
    var that = this;
    that.setData({
      Dflag: 1
    });
  },
  DchangeColor2: function () {
    var that = this;
    that.setData({
      Dflag: 2
    });
  },
  DchangeColor3: function () {
    var that = this;
    that.setData({
      Dflag: 3
    });
  },
  DchangeColor4: function () {
    var that = this;
    that.setData({
      Dflag: 4
    });
  },
  DchangeColor5: function () {
    var that = this;
    that.setData({
      Dflag: 5
    });
  },
  gotoExpress:function(){
    $service.openPage("../check_express/check_express?prescription_id="+order_id)
  },
  // 页面跳转
  goPage(e) {
    let {type} = e.currentTarget.dataset
    if (type === 'per') {
      $service.openPage(`/pages/user_side/index/index_prescription_details/index_prescription_details?prescription_id=${this.data.prescriptionDetail.id}&prescriptionNo${order_id}2&state=1`)
    } else {
      $service.openPage(`/pages/user_side/index/index_medication_details/index_medication_details?prescription_id=${this.data.prescriptionDetail.id}`)
    }
  },
  // 联系商家
  linkShop() {
    if (!this.data.orderInfo.store_phone) {
      $service.showToast('暂无商家联系方式')
      return
    }
    wx.showModal({
      content: `是否要拨打${this.data.orderInfo.store_phone}`,
      success:(res) => {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: this.data.orderInfo.store_phone //仅为示例，并非真实的电话号码
          }).catch(err => {
            $service.showToast('取消拨打')
          })
        }
      }
    })
  },
  // 获取订单信息
  queryDeliveryInfo() {
    app.http.queryDeliveryInfo({"order_num": order_id}).then(res => {
      let {list} = res.data
      if (list) {
        this.setData({
          deliveryInfo: list[0]
        })
      }
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  // 获取廉江his院内处方详情
  getPrescriptionInfoById() {
    app.http.getPrescriptionInfoById({id: Number(order_id)}).then(res => {
      let prescriptionDetail = {
        prescriptionType: res.data.prescriptionType,
        id: order_id,
        prescriptionPrice: res.data.prescriptionPrice,
        hisMiWholePrice: res.data.hisMiWholePrice || 0
      }
      let orderInfo = {
        loc_x: res.data.lat,
        loc_y: res.data.lng,
        store_phone: res.data.hospitalPhoneNumber,
        indent_total: res.data.prescriptionPrice - res.data.hisMiWholePrice || 0,
        order_sn: res.data.outTradeNo,
        create_time: res.data.createTime,
        payment_time: res.data.payTime,
        pat_name: res.data.patientName,
        pat_photo: res.data.patientPhone,
        indent_state: Number(res.data.payState),
        group_name: res.data.hospitalName,
        group_address: res.data.hospitalAddress
      }
      this.setData({
        prescriptionDetail,
        orderInfo,
        prescriptionList: res.data.prescriptionInfoVOList,
        choiceDrugAddress: res.data.hospitalAddress,
        choiceDrugDistance: this.data.choiceDrugDistance
      })
    }).catch(err => {
      console.log(err)
      $service.showToast(err.msg)
    })
  }
})